package com.example.foldertreeview;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

//viewPager构造器
public class ViewPagerAdapter extends FragmentPagerAdapter {

    //传入的展示在viewPager中的页面集合
    private List<Fragment> fragmentList;
    String[] tabTitles;


    public ViewPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList, String[] tabLayoutList) {
        super(fm);
        this.fragmentList = fragmentList;
        this.tabTitles = tabLayoutList;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    /**
     * 不要为ViewPager手动使用addTab()方法添加标题,否则不显示，而应该先创建一个List集合，将其设置在PagerAdapter的getPageTitle方法中
     * @param position
     * @return
     */
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }
}
